import http from '@/utils/http'
import type { AclUser } from './types'

const api_name = '/admin/acl/user'

export function getUserList(page: number, limit: number, username?: string) {
  return http.get<AclUser.PageQueryUserListResp>(
    `${api_name}/${page}/${limit}`,
    { username },
  )
}

export function addUser(data: AclUser.AddUserReq) {
  return http.post<null>(`${api_name}/save`, data)
}

export function updateUser(data: AclUser.AddUserReq) {
  return http.put<null>(`${api_name}/update`, data)
}

export function getUserRole(id: number) {
  return http.get<AclUser.GetUserRoleData>(`${api_name}/toAssign/${id}`)
}

export function assignUserRole(userId: number, roleId: number[]) {
  return http.post<null>(
    `${api_name}/doAssign`,
    `adminId=${userId}&roleId=${roleId.join(',')}`,
    {
      // 请求头
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    },
  )
}

export function batchRemoveUsers(idList: number[]) {
  return http.delete<null>(`${api_name}/batchRemove`, idList)
}
